package com.zzh.lib.xpopup.ea;


import android.util.SparseArray;

import com.zzh.lib.views.adapter.holder.HSuperViewHolder;

import java.util.List;

/**
 * Created by ZZH on 2022/12/8.
 *
 * @Date: 2022/12/8
 * @Email: zzh_hz@126.com
 * @QQ: 1299234582
 * @Author: zzh
 * @Description:
 */
public class ItemDelegateManager<T> {
    private SparseArray<ItemDelegate<T>> delegates = new SparseArray<>();

    private int itemViewDelegateCount;

    public int getItemViewDelegateCount() {
        return itemViewDelegateCount = delegates.size();
    }

    public ItemDelegateManager<T> addDelegate(ItemDelegate<T> delegate) {
        int viewType = delegates.size();
        delegates.put(viewType, delegate);
        viewType++;
        return this;
    }

    public ItemDelegateManager<T> addDelegate(int viewType, ItemDelegate<T> delegate) {
        if (delegates.get(viewType) != null) {
            throw new IllegalArgumentException(
                    "An ItemDelegate is already registered for the viewType = "
                            + viewType
                            + ". Already registered ItemDelegate is "
                            + delegates.get(viewType));
        }
        delegates.put(viewType, delegate);
        return this;
    }

    public ItemDelegateManager<T> removeDelegate(ItemDelegate<T> delegate) {
        int indexToRemove = delegates.indexOfValue(delegate);

        if (indexToRemove >= 0) {
            delegates.removeAt(indexToRemove);
        }
        return this;
    }

    public ItemDelegateManager<T> removeDelegate(int itemType) {
        int indexToRemove = delegates.indexOfKey(itemType);

        if (indexToRemove >= 0) {
            delegates.removeAt(indexToRemove);
        }
        return this;
    }

    public int getItemViewType(T item, int position) {
        int delegatesCount = delegates.size();
        for (int i = 0; i < delegatesCount; i++) {
            ItemDelegate<T> delegate = delegates.valueAt(i);
            if (delegate.isThisType(item, position)) {
                return delegates.keyAt(i);
            }
        }
        return 0;

    }

    public void convert(HSuperViewHolder holder, T item, int position, List<Object> payloads) {
        int delegatesCount = delegates.size();
        for (int i = 0; i < delegatesCount; i++) {
            ItemDelegate<T> delegate = delegates.valueAt(i);
            if (delegate.isThisType(item, position)) {
                if (payloads.isEmpty()) {
                    delegate.bind(holder, item, position);
                } else {
                    delegate.bindWithPayloads(holder, item, position, payloads);
                }
                return;
            }
        }
    }


    public ItemDelegate<T> getItemViewDelegate(int viewType) {
        return delegates.get(viewType);
    }

    public int getItemLayoutId(int viewType) {
        return getItemViewDelegate(viewType).getLayoutId();
    }

    public int getItemViewType(ItemDelegate<T> itemViewDelegate) {
        return delegates.indexOfValue(itemViewDelegate);
    }
}
